package com.ls.blog.security.handler;

import com.alibaba.fastjson.JSON;
import com.ls.blog.common.enums.ResultCode;
import com.ls.blog.utils.R;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: graduationDesign
 * 权限拒绝处理
 * @author: lishun
 * @create: 2021-09-30 16:06
 **/
@Component
public class CustomizeAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        response.setContentType("text/json;charset=utf-8");
        // 状态码设为403
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        response.getWriter().write(JSON.toJSONString(R.fail(ResultCode.PER_DENY)));
    }
}
